<?php

namespace App\Http\Middleware;

use Closure;

class EnsureEmailIsVerified
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 三个判断：
        // 1. 如果用户已经登录
        // 2. 并且email_verified_at不为null
        // 3. 并且访问的不是 email 验证相关 URL 或者退出的 URL。  已经删除
        //dd(!$request->user()->hasVerifiedEmail());


        if ($request->user() && !$request->user()->email_verified_at) {
            // 根据客户端返回对应的内容

            return $request->expectsJson() ? abort(403, 'Your email address is not verified.') : redirect(route('verification.notice'));

        }

        return $next($request);
    }
}
